#include<allegro.h>
#include<time.h>
struct mario{
	int x;
	int y;
	BITMAP *img_right[3];
	BITMAP *img_left[3];
};
struct goro{
	int x;
	int y;
	BITMAP *img_right[3];
	BITMAP *img_left[3];
};

typedef struct mario mario;
typedef struct goro goro;

void attendre(float temps);
int main(){

     int compteur;
	int i,j=0;
	BITMAP *buffer;
	BITMAP *image;
	BITMAP *image2;
	BITMAP *background;
	mario MAR;
	goro goro ;
	allegro_init();
	
	if(install_keyboard() ==-1){
		allegro_message("ERROR! %s",allegro_error);
		return -1;
	}
	
	
	set_color_depth(16);
	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED,1280,1024,0,0) !=0){
		set_gfx_mode(GFX_TEXT,0,0,0,0);
		allegro_message("unable to intialize video mode!\n%s\n",allegro_error);
	return 1;}
	
	
	buffer = create_bitmap(1280,1024);
	background=load_bitmap("./images/sc_ne_.bmp",NULL);
	MAR.img_right[0]=load_bitmap("./images/12.bmp",NULL);
	MAR.img_right[1]=load_bitmap("./images/12.bmp",NULL);
	MAR.img_right[2]=load_bitmap("./images/12.bmp",NULL);
	MAR.img_left[0]=load_bitmap("./images/left0.bmp",NULL);
	MAR.img_left[1]=load_bitmap("./images/left1.bmp",NULL);
	MAR.img_left[2]=load_bitmap("./images/left2.bmp",NULL);
	
	
	goro.img_right[0]=load_bitmap("./images/droit.bmp",NULL);
	goro.img_right[1]=load_bitmap("./images/droit0.bmp",NULL);
	goro.img_right[2]=load_bitmap("./images/droit1.bmp",NULL);
	goro.img_left[0]=load_bitmap("./images/gauche.bmp",NULL);
	goro.img_left[1]=load_bitmap("./images/gauche0.bmp",NULL);
	goro.img_left[2]=load_bitmap("./images/gauche1.bmp",NULL);
	
	
	for(i=0;i<3;i++){
	
		if (MAR.img_right[i]==NULL && background==NULL && MAR.img_left[i]==NULL)
		{allegro_message("could not load images");
	 	allegro_exit();}}
	 	
	MAR.x=SCREEN_W/2;
	MAR.y=SCREEN_H/5;
	
	goro.x=SCREEN_W/4;
	goro.y=SCREEN_H/3;
	
	blit(background,buffer,0,0,0,0,background->w,background->h);
	draw_sprite(buffer,MAR.img_right[0],MAR.x,MAR.y);
	draw_sprite(buffer,goro.img_right[0],goro.x,goro.y);
	blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
	i=0;
	
	image=MAR.img_right[0];
	image2=goro.img_left[0];
	while (!key[KEY_ESC])
	{blit(background,buffer,0,0,0,0,background->w,background->h);
	    if(MAR.x  < goro.x){for(i=0;i<3;i++){image2=goro.img_left[i];
	            							  goro.x -=i;
	                                          draw_sprite(buffer,image2,goro.x,goro.y);
	                                          draw_sprite(buffer,image,MAR.x,MAR.y);
	                                          blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H); 
	    }
	    i=0;
	    }
	
	
	
		if (key[KEY_RIGHT] && MAR.x < SCREEN_W -50)
		{i++;
		if(i>2){i=0;}
		 image=MAR.img_right[i];
		 image2=goro.img_right[i];

		 draw_sprite(buffer,image,MAR.x,MAR.y);
		 draw_sprite(buffer,image2,goro.x,goro.y);
		 blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		 MAR.x += 1;
		 goro.x -=1;
		}
		 if (key[KEY_LEFT] && MAR.x > 0)
		{
		
		 image=MAR.img_left[i];
		 i++;
		 blit(background,buffer,0,0,0,0,background->w,background->h);
		 draw_sprite(buffer,image,MAR.x,MAR.y);
		 draw_sprite(buffer,image2,goro.x,goro.y);
		 blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		 MAR.x -= 1;
		if(i>2){i=0;}
		}
		if(key[KEY_UP] && MAR.y < SCREEN_H)
		
		{for(i=0;i<22;i++){
		MAR.y -= i;
		blit(background,buffer,0,0,0,0,background->w,background->h);
		draw_sprite(buffer,image,MAR.x,MAR.y);
		draw_sprite(buffer,image2,goro.x,goro.y);
		blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		 for(compteur=3;compteur>0;compteur--){
 
        attendre(0.0005);
    }
		}
		for(i=0;i<22;i++){
		MAR.y += i;
		blit(background,buffer,0,0,0,0,background->w,background->h);
		draw_sprite(buffer,image,MAR.x,MAR.y);
		draw_sprite(buffer,image2,goro.x,goro.y);
		blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		for(compteur=3;compteur>0;compteur--){
 
        attendre(0.0005);
    }
		}i=0;}
		
		
		if(key[KEY_A] && MAR.y < SCREEN_H && MAR.x < SCREEN_W -50)
		{
		for(i=0;i<22;i++){
		j=j+1;
		MAR.y -= i;
		MAR.x += j/2;
		blit(background,buffer,0,0,0,0,background->w,background->h);
		draw_sprite(buffer,image,MAR.x,MAR.y);
		draw_sprite(buffer,image2,goro.x,goro.y);
		blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		 for(compteur=3;compteur>0;compteur--){
 
        attendre(0.0005);
    }
		}
		for(i=0;i<22;i++){
		MAR.y += i;
		MAR.x += j/2;
		blit(background,buffer,0,0,0,0,background->w,background->h);
		draw_sprite(buffer,image,MAR.x,MAR.y);
		draw_sprite(buffer,image2,goro.x,goro.y);
		blit(buffer, screen, 0,0,0,0,SCREEN_W, SCREEN_H);
		for(compteur=3;compteur>0;compteur--){
 
        attendre(0.0005);
    }
		}i=0;
		 j=0;}
		
		
		
			 	
	}	
	readkey();
	destroy_bitmap(buffer);
	return 0;
	}
 
    
 
void attendre(float temps)
{
    clock_t arrivee=clock()+(temps*CLOCKS_PER_SEC); 
 
    while(clock()<arrivee);
}
